<div id="form"></div>
<script>
AdminPagesFuncs.init({
	pages: {{JSON:dictionaries.pages}},
	tplaces: {{JSON:dictionaries.template_places}},
	cpage_content: {{JSON:cpage_blocks}},
	l10ns: {{JSON:l10n_all}}
});

var leftcolw=360;
var rightcolw;
var twocol=true;

var frm=$("#form").ajajform({
	header: "{{!Edit page}}",
	layouttemplate: '<div class="af-peditcol1"></div><div class="af-peditcol2"></div><div style="clear: both;"></div>',
	submit: {
		url: '{{action}}',
		value: "{{!Save and exit}}",
		ajaj: true,
		click: function() { frm.ajajform("setSubmitParam","save_only",""); return 1; },
		params: {mode: "save",id: '{{fields.id}}',ajaj: 1}
	},
	footerButtons: [{value: "{{!Save and continue}}", click: function() { frm.ajajform("setSubmitParam","save_only","1");frm.ajajform("submitClicked"); } }]
});
AdminPagesFuncs.window_resize();
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "select",
	name: "template_id",
	label: "{{!Page template}}",
	values: {{JSON:dictionaries.templates}},
	validate: function(x) {console.log("(validate) template_id - "+x); if (!x || x=="" || x=="0") return "{{!Template must be chosen}}";},
	change: function() {call_page_editor_rebuild_blocks();},
	width: leftcolw,
	cssclass: "afw-select leftcolw1",
	value: "{{cpage.template_id}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type:"select",name:"parent_id",label:"{{!Parent page}}",values:AdminPagesFuncs.pages,width:leftcolw,change:function(){AdminPagesFuncs.try_update_alias();},cssclass:"afw-select leftcolw1",value:"{{cpage.parent_id}}",add_null:1});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "textedit",
	name: "alias",
	label: "{{!URL}}",
	validate: function(x) {
			if (!x || x.length==0) return "{{!URL must be not empty}}";
			if (!x.match(/^\//)) return "{{!URL must start with slash}}";
			if (!x.match(/\/$/)) return "{{!URL must end with slash}}";
			if (x.match(/\/\//)) return "{{!URL invalid}}";
			if (!x.match(/^\/([\w_\-]+\/)+$/)) return "{{!URL invalid}}";
	},
	width: leftcolw-10,
	cssclass: "afw-textedit leftcolw2",
	value: {{JSON:cpage.alias}}
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "textedit",name: "prerun",label: "{{!Prerun controller}}",width: leftcolw-10,cssclass: "afw-textedit leftcolw2",value: {{JSON:cpage.prerun}}});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "textareaedit",name: "prerun_param",label: "{{!Prerun param}}",height: 60,width: leftcolw-10,cssclass: "afw-textareaedit leftcolw2",value: {{JSON:cpage.prerun_param}}});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "checkbox2",name: "show_in_nav",label: "{{!Show in navigation}}",width: leftcolw-10,cssclass: "afw-checkbox leftcolw2",value: "{{cpage.show_in_nav}}"});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "checkbox2",name: "hide_anon",label: "{{!Deny anonymous users}}",width: leftcolw-10,cssclass: "afw-checkbox leftcolw2",value: "{{cpage.hide_anon}}"});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "checkbox2",name: "hide_reg",label: "{{!Deny registered users}}",width: leftcolw-10,cssclass: "afw-checkbox leftcolw2",value: "{{cpage.hide_reg}}"});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "select",name: "role_id",label: "{{!Allow only role}}",values: {{JSON:dictionaries.roles}},width: leftcolw,cssclass: "afw-select leftcolw1",value: "{{cpage.role_id}}",add_null:1});
frm.ajajform("insertWidget",'.af-peditcol1',null,{type: "textedit",name: "cssclass",label: "{{!CSS Class}}",width: leftcolw-10,cssclass: "afw-textedit leftcolw2",value: {{JSON:cpage.cssclass}}});
frm.ajajform("insertWidget",".af-peditcol2",null,{type:"accordion",name:"l10n_tabs",mode:"tabs",label:"",pages:{{JSON:l10n_pages}}});

{{foreach l10n in l10n_all}}
ajajform_add_inputs(frm,[
	{type:"textedit",name:"{{l10n.id}}__name",label:"{{!Page name}}",value:{{JSON:ADMINGETL("name"):cpage}},xpath:".l10n-{{l10n.id}}-block",width:leftcolw-10},
	{type:"textedit",name:"{{l10n.id}}__title",label:"{{!Page title}}",value:{{JSON:ADMINGETL("title"):cpage}},xpath:".l10n-{{l10n.id}}-block",width:leftcolw-10},
	{type:"textedit",name:"{{l10n.id}}__meta_description",label:"{{!meta description}}",value:{{JSON:ADMINGETL("meta_description"):cpage}},xpath:".l10n-{{l10n.id}}-block",width:leftcolw-10},
	{type:"textedit",name:"{{l10n.id}}__meta_keywords",label:"{{!meta keywords}}",value:{{JSON:ADMINGETL("meta_keywords"):cpage}},xpath:".l10n-{{l10n.id}}-block",width:leftcolw-10},
	{type:"checkbox2",name:"{{l10n.id}}__disabled_l10n",label:"{{!Disabled}}",value:{{JSON:ADMINGETL("disabled_l10n"):cpage}},xpath:".l10n-{{l10n.id}}-block",width:leftcolw-10}
]);
{{/foreach}}
function call_page_editor_rebuild_blocks()
{
	AdminPagesFuncs.rebuild_blocks(frm,AdminPagesFuncs.tplaces[frm.ajajform("getWidgetValue","template_id")]);
}
call_page_editor_rebuild_blocks();


// Setting autoupdate alias on name change
var alias_set='{{fields.id}}'.length?1:0;
var namefield;
for (var k in frm.ajajform("getWidgets")) if (!namefield && k.match(/^\d+__name$/)) namefield=k;
if (namefield) frm.ajajform("setWidgetOption",namefield,"change",function() {AdminPagesFuncs.try_update_alias(); });


</script>

